package com.jic.member.impl;

import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.AdminOperate;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.manage.SysDeptApi;
import com.jic.manager.api.rest.ManagerChildApi;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.*;
import com.jic.member.enums.CONSUME_CODE;
import com.jic.member.enums.EXCEPTION;
import com.jic.member.enums.MODE_CODE;
import com.jic.member.mapper.*;
import com.jic.member.request.*;
import com.jic.member.request.MememberIntegraIncrement;
import com.jic.member.response.MememberIntegralRuleResponse;
import com.jic.member.service.MememberGrowthAppraisalService;
import com.jic.member.service.MememberIntegralRuleService;
import com.jic.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 会员积分规则主表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-04-02 03:38:35
 */
@Slf4j
@Service
public class MememberIntegralRuleServiceImpl implements MememberIntegralRuleService {

    private final MememberIntegralRuleMapper mememberIntegralRuleMapper;

    private final MememberIntegralGiveConsumeMapper mememberIntegralGiveConsumeMapper;
    @Resource
    private MememberIntegralBaseMapper mememberIntegralBaseMapper;

    @Autowired
    MememberGrowthAppraisalService mememberGrowthAppraisalService;

    @Resource
    MememberIntegralActionMapper mememberIntegralActionMapper;

    @Resource
    MememberIntegralOrderRechargeMapper mememberIntegralOrderRechargeMapper;

    @Resource
    MememberIntegralOrderIncrementMapper mememberIntegralOrderIncrementMapper;


    @Resource
    MememberIntegralExchangeCouponMapper mememberIntegralExchangeCouponMapper;

    @Autowired
    private SysDeptApi sysDeptApi;


    public MememberIntegralRuleServiceImpl(MememberIntegralRuleMapper mememberIntegralRuleMapper, MememberIntegralGiveConsumeMapper mememberIntegralGiveConsumeMapper) {
        this.mememberIntegralRuleMapper = mememberIntegralRuleMapper;
        this.mememberIntegralGiveConsumeMapper = mememberIntegralGiveConsumeMapper;
    }

    @Override
    public long insert(MememberIntegralRule mememberIntegralRule) {
        long flag = mememberIntegralRuleMapper.insert(mememberIntegralRule);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return mememberIntegralRuleMapper.delete(id);
    }

    @Override
    public long update(MememberIntegralRule mememberIntegralRule) {

        return mememberIntegralRuleMapper.update(mememberIntegralRule);
    }

    @Override
    public MememberIntegralRule queryByPrimaryKey(Long ruleId) {
        return mememberIntegralRuleMapper.queryByPrimaryKey(ruleId);
    }

    @Override
    public List<MememberIntegralRule> query(MememberIntegralRule mememberIntegralRule) {
        return mememberIntegralRuleMapper.query(mememberIntegralRule);
    }

    /**
     * 查询平台积分抵现规
     *
     * @param mememberIntegralRule
     * @param pageQuery
     * @return
     */
    @Override
    public PageResult<MememberIntegralRule> queryPage(MememberIntegralRule mememberIntegralRule, Page pageQuery) {
        log.info("开始查询平台积分抵现规则入参,{},{}", JSON.toJSONString(mememberIntegralRule), JSON.toJSONString(pageQuery));
        AdminOperate admin = mememberIntegralRule.getAdminOperate();
        log.info("查询平台积分抵现规则==》参数：操作信息,{}", admin);
        mememberIntegralRule.setDeptId(admin.getDeptId());
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<MememberIntegralRule> list = mememberIntegralRuleMapper.queryPage(startIndex, pageQuery.getLimit(), mememberIntegralRule);
        log.info("结束查询平台积分抵现规则列表,{}", JSON.toJSONString(list));
        log.info("开始查询平台积分抵现规则总数==》入参,{}", JSON.toJSONString(mememberIntegralRule));
        long count = mememberIntegralRuleMapper.queryPageCount(mememberIntegralRule);
        log.info("结束查询平台积分抵现规则总数==》结果,{}", count);

        list.forEach(items -> {
            RestResult<String> name = sysDeptApi.queryDeptNameById(items.getDeptId());
            items.setDeptId(admin.getDeptId());
            items.setDeptName(name.getData());

        });
        log.info("结束遍历规则列表查询组织名称,{}");
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }


    /**
     * 更新状态
     *
     * @param form
     * @return
     */
    public long updateStatus(MememberIntegralRuleUpdateRequest form) {
        MememberIntegralRule memberRequest = BeanUtil.copy(form, MememberIntegralRule.class);
        return mememberIntegralRuleMapper.updateStatus(memberRequest);
    }


    /**
     * 添加积分信息关联会员积分注册赠送-消费抵现表
     *
     * @param request
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public RestResult addIntegraRuleInfo(MememberIntegralRule request) {
        RestResult restResult = new RestResult();
        AdminOperate admin = request.getAdminOperate();

        MememberIntegralRule requestInfo = getRequestInfo(request, admin);

        String getDate = DateUtil.getTodayDate();
        final int i = (int) ((Math.random() * 9 + 1) * 1000);
        String mun = "JF" + getDate + i;
        if (requestInfo.getRuleType() == null || requestInfo.getRuleType() == "") {
            requestInfo.setRuleType("1");
        }
        requestInfo.setRuleNum(mun);
        requestInfo.setStatus(0);
        requestInfo.setDeleteFlag(0);
        final MememberIntegralBase mememberIntegralBaseinfo = getMememberIntegralBaseinfo();

        //下单赠送
        if (MODE_CODE.ORDER.getCode().equals(request.getObtainMode())) {
            requestInfo.setObtainModeName(requestInfo.getObtainModeName());
            final Long id = mememberIntegralRuleMapper.insert(requestInfo);
            final Long ruleId = request.getRuleId();
            return saveOrderRechargeInfo(ruleId, id, admin, request);

        }
        //分注册赠送-消费抵现
        else if (MODE_CODE.GIVE.getCode().equals(request.getObtainMode()) || CONSUME_CODE.ORDER.getCode().equals(request.getConsumeMode())) {

            if (MODE_CODE.GIVE.getCode().equals(request.getObtainMode())) {
                requestInfo.setObtainModeName(MODE_CODE.GIVE.getMessage());
            }
            if (MODE_CODE.ORDER.getCode().equals(request.getObtainMode())) {
                requestInfo.setObtainModeName(MODE_CODE.ORDER.getMessage());
            }
            final Long id = mememberIntegralRuleMapper.insert(requestInfo);
            final Long ruleId = request.getRuleId();
            if (id != null && ruleId != null) {
                MememberIntegralGiveConsume IntegralGiveConsume = new MememberIntegralGiveConsume();
                //规则主表ID
                IntegralGiveConsume.setRuleId(ruleId);
                //注册发放数量
                IntegralGiveConsume.setGiveIntegralNum(request.getGiveIntegralNum());
                IntegralGiveConsume.setDeptId(admin.getDeptId());
                IntegralGiveConsume.setStatus(1);
                if (StringUtils.isNotEmpty(request.getConsumeMode())) {
                    IntegralGiveConsume.setConsumeMode(Long.valueOf(request.getConsumeMode()));
                    IntegralGiveConsume.setConsumeIntegralMaxNum(request.getConsumeIntegralMaxNum());
                }else {
                    IntegralGiveConsume.setConsumeIntegralMaxNum(request.getGiveIntegralNum());
                }
                Long insert = mememberIntegralGiveConsumeMapper.insert(IntegralGiveConsume);
                if (insert != null) {
                    restResult.setCode(EXCEPTION.SUCCESS.getName());
                }
            }
            return restResult;
        }

        //分享赠送
        else if (MODE_CODE.SHARE.getCode().equals(request.getObtainMode())) {
            if (MODE_CODE.SHARE.getCode().equals(request.getObtainMode())) {
                requestInfo.setObtainModeName(MODE_CODE.SHARE.getMessage());
            }
            final Long id = mememberIntegralRuleMapper.insert(requestInfo);
            final Long ruleId = request.getRuleId();
            return saveInfo(ruleId, id, admin, request);
        }
        //举办活动
        else if (MODE_CODE.HOLD.getCode().equals(request.getObtainMode())) {
            if (MODE_CODE.HOLD.getCode().equals(request.getObtainMode())) {
                requestInfo.setObtainModeName(MODE_CODE.HOLD.getMessage());
            }
            final Long id = mememberIntegralRuleMapper.insert(requestInfo);
            final Long ruleId = request.getRuleId();
            return saveInfo(ruleId, id, admin, request);
        }
        //参加活动
        else if (MODE_CODE.JOIN.getCode().equals(request.getObtainMode())) {
            if (MODE_CODE.JOIN.getCode().equals(request.getObtainMode())) {
                requestInfo.setObtainModeName(MODE_CODE.JOIN.getMessage());
            }
            final Long id = mememberIntegralRuleMapper.insert(requestInfo);
            final Long ruleId = request.getRuleId();
            return saveInfo(ruleId, id, admin, request);
        }
        //点赞赠送
        if (MODE_CODE.PRAISE.getCode().equals(request.getObtainMode())) {
            if (MODE_CODE.PRAISE.getCode().equals(request.getObtainMode())) {
                requestInfo.setObtainModeName(MODE_CODE.PRAISE.getMessage());
            }
            final Long id = mememberIntegralRuleMapper.insert(requestInfo);
            final Long ruleId = request.getRuleId();
            return saveInfo(ruleId, id, admin, request);
        }
        //收藏赠送
        else if (MODE_CODE.COLLECTION.getCode().equals(request.getObtainMode())) {
            if (MODE_CODE.COLLECTION.getCode().equals(request.getObtainMode())) {
                requestInfo.setObtainModeName(MODE_CODE.COLLECTION.getMessage());
            }
            final Long id = mememberIntegralRuleMapper.insert(requestInfo);
            final Long ruleId = request.getRuleId();
            return saveInfo(ruleId, id, admin, request);
        }
        //嗮单赠送
        else if (MODE_CODE.SUN.getCode().equals(request.getObtainMode())) {
            if (MODE_CODE.SUN.getCode().equals(request.getObtainMode())) {
                requestInfo.setObtainModeName(MODE_CODE.SUN.getMessage());
            }
            final Long id = mememberIntegralRuleMapper.insert(requestInfo);
            final Long ruleId = request.getRuleId();
            return saveInfo(ruleId, id, admin, request);
        }
        //评价赠送
        else if (MODE_CODE.EVALUATE.getCode().equals(request.getObtainMode())) {
            if (MODE_CODE.EVALUATE.getCode().equals(request.getObtainMode())) {
                requestInfo.setObtainModeName(MODE_CODE.EVALUATE.getMessage());
            }
            final Long id = mememberIntegralRuleMapper.insert(requestInfo);
            final Long ruleId = request.getRuleId();
            return saveInfo(ruleId, id, admin, request);
        }
        //充值
        else if (MODE_CODE.RECHARGE.getCode().equals(request.getObtainMode())) {
            if (MODE_CODE.RECHARGE.getCode().equals(request.getObtainMode())) {
                requestInfo.setObtainModeName(MODE_CODE.RECHARGE.getMessage());
            }
            final Long id = mememberIntegralRuleMapper.insert(requestInfo);
            final Long ruleId = request.getRuleId();
            return saveRechargeInfo(ruleId, id, admin, request);
        }

        //兑换优惠券或红包
        else if (CONSUME_CODE.GIVE.getCode().equals(request.getConsumeMode()) || CONSUME_CODE.COUPON.getCode().equals(request.getConsumeMode())) {
            if (CONSUME_CODE.GIVE.getCode().equals(request.getConsumeMode())) {
                requestInfo.setObtainModeName(CONSUME_CODE.GIVE.getMessage());
            }
            if (CONSUME_CODE.COUPON.getCode().equals(request.getConsumeMode())) {
                requestInfo.setObtainModeName(CONSUME_CODE.COUPON.getMessage());
            }
            final Long id = mememberIntegralRuleMapper.insert(requestInfo);
            final Long ruleId = request.getRuleId();
            return saveExchangeInfo(ruleId, id, admin, request);
        }
        //自定义获取
        else if (null != request.getObtainMode() && Long.valueOf(request.getObtainMode()) > 8 || null != request.getConsumeMode() && Long.valueOf(request.getConsumeMode()) > 4) {
            requestInfo.setObtainModeName(request.getObtainModeName());
            final Long id = mememberIntegralRuleMapper.insert(requestInfo);
            final Long ruleId = request.getRuleId();
            return saveInfo(ruleId, id, admin, request);
        }
        return restResult;
    }

    /**
     * 充值赠送
     *
     * @param ruleId
     * @param id
     * @param admin
     * @param request
     */
    @Transactional(rollbackFor = Exception.class)
    RestResult saveRechargeInfo(Long ruleId, Long id, AdminOperate admin, MememberIntegralRule request) {
        MememberIntegralOrderRecharge recharge = new MememberIntegralOrderRecharge();
        RestResult restResult = new RestResult();
        if (ruleId != null && id != null) {
            if (1 == request.getConsumeFlag()) {
                recharge.setDeptId(admin.getDeptId());
                recharge.setRuleId(ruleId);
                recharge.setOrderRechargeId(ruleId);
                recharge.setConsumeFlag(1);
                recharge.setGiveType(2);
                recharge.setGiveTypeName(MODE_CODE.RECHARGE.getMessage());
                recharge.setConsumeNum(request.getConsumeNum());
                recharge.setGiveIntegralNum(request.getGiveIntegralNum());
                recharge.setGiveGrowthMaxNum(request.getGiveGrowthMaxNum());
                recharge.setGiveIntegralMaxNum(request.getGiveIntegralMaxNum());
                recharge.setCreator(admin.getOperator());
                recharge.setCreatorName(admin.getOperateName());
                recharge.setUpdater(admin.getOperator());
                recharge.setUpdaterName(admin.getOperateName());
                recharge.setStatus(1);
                recharge.setDeleteFlag(0);
                final long value = mememberIntegralOrderRechargeMapper.insert(recharge);
                if (value > 0) {
                    restResult.setCode(EXCEPTION.SUCCESS.getName());
                }
            } else if (2 == request.getConsumeFlag()) {
                return saveRechargeIncrementInfo(ruleId, id, admin, request);

            }

        }
        return restResult;
    }

    /**
     * 保存充值阶梯信息
     *
     * @param ruleId
     * @param id
     * @param admin
     * @param request
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    RestResult saveRechargeIncrementInfo(Long ruleId, Long id, AdminOperate admin, MememberIntegralRule request) {
        MememberIntegralOrderRecharge recharge = new MememberIntegralOrderRecharge();
        RestResult restResult = new RestResult();
        MememberIntegralOrderIncrement rechargeIncrement = new MememberIntegralOrderIncrement();
        recharge.setDeptId(admin.getDeptId());
        recharge.setRuleId(ruleId);
        recharge.setConsumeFlag(2);
        recharge.setConsumeNum(request.getConsumeNum());
        recharge.setGiveIntegralNum(request.getGiveIntegralNum());
        recharge.setGiveGrowthMaxNum(request.getGiveGrowthMaxNum());
        recharge.setGiveIntegralMaxNum(request.getGiveIntegralMaxNum());
        recharge.setGiveType(2);
        recharge.setGiveTypeName("阶梯性" + MODE_CODE.RECHARGE.getMessage());
        recharge.setCreator(admin.getOperator());
        recharge.setCreatorName(admin.getOperateName());
        recharge.setUpdater(admin.getOperator());
        recharge.setUpdaterName(admin.getOperateName());
        recharge.setStatus(1);
        recharge.setDeleteFlag(0);
        final Long orderRechargeId = mememberIntegralOrderRechargeMapper.insert(recharge);
        if (orderRechargeId != null) {
            restResult.setCode(EXCEPTION.SUCCESS.getName());
            restResult.setData(orderRechargeId);
            List<MememberIntegraIncrement> mememberIntegraIncrement = request.getMememberIntegraIncrement();
            rechargeIncrement.setRuleId(ruleId);
            rechargeIncrement.setDeptId(admin.getDeptId());
            rechargeIncrement.setOrderRechargeId(recharge.getOrderRechargeId());
            rechargeIncrement.setCreatorName(admin.getOperateName());
            rechargeIncrement.setUpdaterName(admin.getOperateName());
            rechargeIncrement.setGiveIntegralMaxNum(request.getGiveIntegralMaxNum());
            rechargeIncrement.setStatus(1);
            rechargeIncrement.setDeleteFlag(0);
            mememberIntegraIncrement.removeAll(Collections.singleton(null));
            Iterator it = mememberIntegraIncrement.iterator();
            while (it.hasNext()) {
                if (it.next() == null) {
                    it.remove();
                }
            }
            mememberIntegraIncrement.forEach(items -> {
                rechargeIncrement.setConsumeNum(items.getConsumeNum());
                rechargeIncrement.setGiveIntegralNum(items.getGiveIntegralNum());
                mememberIntegralOrderIncrementMapper.insert(rechargeIncrement);
            });
            restResult.setCode(EXCEPTION.SUCCESS.getName());

        }
        return restResult;
    }


    /**
     * 兑换优惠券或红包
     *
     * @param ruleId
     * @param id
     * @param admin
     * @param request
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    RestResult saveExchangeInfo(Long ruleId, Long id, AdminOperate admin, MememberIntegralRule request) {
        MememberIntegralExchangeCoupon exchangeCoupon = new MememberIntegralExchangeCoupon();
        RestResult restResult = new RestResult();

        if (ruleId != null && id != null) {
            exchangeCoupon.setDeptId(admin.getDeptId());
            exchangeCoupon.setRuleId(ruleId);
            exchangeCoupon.setExchangeMoney(request.getExchangeMoney());
            exchangeCoupon.setExchangeIntegral(request.getExchangeIntegral());
            if (request.getExchangeType() == 1) {
                exchangeCoupon.setExchangeName("比例兑换");
            }
            if (request.getExchangeType() == 2) {
                exchangeCoupon.setExchangeName("固定数额兑换");
            }
            exchangeCoupon.setExchangeType(request.getExchangeType());
            exchangeCoupon.setConsumeIntegralNum(request.getConsumeIntegralMaxNum());
            exchangeCoupon.setExchangeCouponRedEnvelopes(request.getExchangeCouponRedEnvelopes());
            exchangeCoupon.setCreator(admin.getOperator());
            exchangeCoupon.setCreatorName(admin.getOperateName());
            exchangeCoupon.setUpdaterName(admin.getOperateName());
            exchangeCoupon.setUpdaterId(admin.getOperator());
            exchangeCoupon.setStatus(1);
            exchangeCoupon.setDeleteFlag(0);
            exchangeCoupon.setConsumeIntegralNum(request.getConsumeIntegralNum());
            final long insert = mememberIntegralExchangeCouponMapper.insert(exchangeCoupon);
            if (insert > 0) {
                restResult.setCode(EXCEPTION.SUCCESS.getName());
                restResult.setData(insert);
            }
        }
        return restResult;
    }

    /**
     * 保存下单
     *
     * @param ruleId
     * @param id
     * @param admin
     * @param request
     */
    @Transactional(rollbackFor = Exception.class)
    RestResult saveOrderRechargeInfo(Long ruleId, Long id, AdminOperate admin, MememberIntegralRule request) {
        RestResult restResult = new RestResult();
        MememberIntegralOrderRecharge recharge = new MememberIntegralOrderRecharge();
        if (ruleId != null && id != null) {
            //每消费赠送
            if (1 == request.getOrderFlag()) {
                recharge.setDeptId(admin.getDeptId());
                recharge.setRuleId(ruleId);
                recharge.setOrderRechargeId(ruleId);
                recharge.setConsumeFlag(1);
                recharge.setConsumeNum(request.getConsumeNum());
                recharge.setGiveIntegralNum(request.getGiveIntegralNum());
                recharge.setGiveGrowthMaxNum(request.getGiveGrowthMaxNum());
                recharge.setGiveIntegralMaxNum(request.getGiveIntegralMaxNum());
                recharge.setCreator(admin.getOperator());
                recharge.setGiveType(1);
                if (1 == request.getOrderFlag()) {
                    recharge.setGiveType(1);
                    recharge.setConsumeFlag(1);
                } else {
                    recharge.setGiveType(2);
                    recharge.setConsumeFlag(2);
                }
                recharge.setGiveTypeName(MODE_CODE.ORDER.getMessage());
                recharge.setCreatorName(admin.getOperateName());
                recharge.setUpdater(admin.getOperator());
                recharge.setUpdaterName(admin.getOperateName());
                recharge.setStatus(1);
                recharge.setDeleteFlag(0);
                final long value = mememberIntegralOrderRechargeMapper.insert(recharge);
                if (value > 0) {
                    restResult.setCode(EXCEPTION.SUCCESS.getName());
                    restResult.setData(recharge);
                    return restResult;
                }
            } else if (2 == request.getOrderFlag()) {
                return saveOrderRechargeIncrementInfo(ruleId, id, admin, request);
            }
            restResult.setCode(EXCEPTION.SUCCESS.getName());
        } else {
            restResult.setCode(EXCEPTION.FAIL.getName());
        }
        return restResult;
    }

    /**
     * 保存会员积分规则下单-充值赠送递增表信息
     *
     * @param ruleId
     * @param id
     * @param admin
     * @param request
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    RestResult saveOrderRechargeIncrementInfo(Long ruleId, Long id, AdminOperate admin, MememberIntegralRule request) {
        RestResult restResult = new RestResult();
        MememberIntegralOrderRecharge recharge = new MememberIntegralOrderRecharge();
        MememberIntegralOrderIncrement orderIncrement = new MememberIntegralOrderIncrement();
        recharge.setDeptId(admin.getDeptId());
        recharge.setRuleId(ruleId);
        recharge.setConsumeFlag(2);
        recharge.setConsumeNum(request.getConsumeNum());
        recharge.setGiveIntegralNum(request.getGiveIntegralNum());
        recharge.setGiveGrowthMaxNum(request.getGiveGrowthMaxNum());
        recharge.setGiveIntegralMaxNum(request.getGiveIntegralMaxNum());
        recharge.setGiveType(2);
        recharge.setGiveTypeName("阶梯性" + MODE_CODE.ORDER.getMessage());
        recharge.setCreator(admin.getOperator());
        recharge.setCreatorName(admin.getOperateName());
        recharge.setUpdater(admin.getOperator());
        recharge.setUpdaterName(admin.getOperateName());
        recharge.setStatus(1);
        recharge.setDeleteFlag(0);
        final Long orderRechargeId = mememberIntegralOrderRechargeMapper.insert(recharge);
        if (orderRechargeId != null) {
            List<MememberIntegraIncrement> mememberIntegraIncrement = request.getMememberIntegraIncrement();
            orderIncrement.setRuleId(ruleId);
            orderIncrement.setDeptId(admin.getDeptId());
            orderIncrement.setOrderRechargeId(recharge.getOrderRechargeId());
            orderIncrement.setCreatorName(admin.getOperateName());
            orderIncrement.setUpdaterName(admin.getOperateName());
            orderIncrement.setGiveIntegralMaxNum(request.getGiveIntegralMaxNum());
            orderIncrement.setStatus(1);
            orderIncrement.setDeleteFlag(0);
            List<MememberIntegraIncrement> list = new ArrayList<>();
            for (int i = 0; i < mememberIntegraIncrement.size(); i++) {
                if (null != mememberIntegraIncrement.get(i).getConsumeNum() && null != mememberIntegraIncrement.get(i).getGiveIntegralNum()) {
                    list.add(mememberIntegraIncrement.get(i));
                }
            }
            list.forEach(items -> {
                orderIncrement.setConsumeNum(items.getConsumeNum());
                orderIncrement.setGiveIntegralNum(items.getGiveIntegralNum());
                mememberIntegralOrderIncrementMapper.insert(orderIncrement);
            });
            restResult.setCode(EXCEPTION.SUCCESS.getName());
            restResult.setData(orderIncrement);
        } else {
            restResult.setCode(EXCEPTION.FAIL.getName());
        }
        return restResult;
    }

    /**
     * 评价-分享-点赞-晒单-收藏赠送表
     *
     * @param ruleId
     * @param id
     * @param admin
     * @param requestInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    RestResult saveInfo(Long ruleId, Long id, AdminOperate admin, MememberIntegralRule requestInfo) {
        MememberIntegralAction mememberIntegralAction = new MememberIntegralAction();
        RestResult restResult = new RestResult();
        if (id != null && ruleId != null) {
            if (null != requestInfo.getObtainMode() && MODE_CODE.SHARE.getCode().equals(requestInfo.getObtainMode())
                    || MODE_CODE.PRAISE.getCode().equals(requestInfo.getObtainMode())
                    || MODE_CODE.COLLECTION.getCode().equals(requestInfo.getObtainMode())
                    || MODE_CODE.SUN.getCode().equals(requestInfo.getObtainMode())
                    || null != requestInfo.getObtainMode() && Long.valueOf(requestInfo.getObtainMode()) > 8
                    || null != requestInfo.getConsumeMode() && Long.valueOf(requestInfo.getConsumeMode()) > 4) {

                mememberIntegralAction.setDeptId(admin.getDeptId());
                mememberIntegralAction.setRuleId(ruleId);
                mememberIntegralAction.setGiveIntegralNum(requestInfo.getGiveIntegralNum());
                mememberIntegralAction.setGiveIntegralMaxNum(requestInfo.getGiveIntegralMaxNum());
                mememberIntegralAction.setGiveGrowthNum(requestInfo.getGiveGrowthNum());
                mememberIntegralAction.setGiveGrowthMaxNum(requestInfo.getGiveGrowthMaxNum());
                mememberIntegralAction.setStatus(0);
                mememberIntegralAction.setStatus(1);
                //mememberIntegralAction.setActionType(Integer.parseInt(requestInfo.getObtainMode()));
                if (null != requestInfo.getObtainMode() && requestInfo.getObtainMode().equals(MODE_CODE.SHARE.getCode())) {
                    mememberIntegralAction.setActionTypeName(MODE_CODE.SHARE.getMessage());
                    mememberIntegralAction.setActionType(Integer.parseInt(MODE_CODE.SHARE.getCode()));
                }
                if (null != requestInfo.getObtainMode() && requestInfo.getObtainMode().equals(MODE_CODE.PRAISE.getCode())) {
                    mememberIntegralAction.setActionTypeName(MODE_CODE.PRAISE.getMessage());
                    mememberIntegralAction.setActionType(Integer.parseInt(MODE_CODE.PRAISE.getCode()));
                }
                if (null != requestInfo.getObtainMode() && requestInfo.getObtainMode().equals(MODE_CODE.COLLECTION.getCode())) {
                    mememberIntegralAction.setActionTypeName(MODE_CODE.COLLECTION.getMessage());
                    mememberIntegralAction.setActionType(Integer.parseInt(MODE_CODE.COLLECTION.getCode()));
                }
                if (null != requestInfo.getObtainMode() && requestInfo.getObtainMode().equals(MODE_CODE.SUN.getCode())) {
                    mememberIntegralAction.setActionTypeName(MODE_CODE.SUN.getMessage());
                    mememberIntegralAction.setActionType(Integer.parseInt(MODE_CODE.SUN.getCode()));
                }
                if (null != requestInfo.getObtainMode() && Long.valueOf(requestInfo.getObtainMode()) > 8) {
                    mememberIntegralAction.setActionType(Integer.valueOf(requestInfo.getObtainMode()));
                }
                if (null != requestInfo.getConsumeMode() && Long.valueOf(requestInfo.getConsumeMode()) > 4) {
                    mememberIntegralAction.setActionType(Integer.valueOf(requestInfo.getConsumeMode()));
                    MememberIntegralGiveConsume IntegralGiveConsume = new MememberIntegralGiveConsume();
                    //规则主表ID
                    IntegralGiveConsume.setRuleId(ruleId);
                    //注册发放数量
                    IntegralGiveConsume.setGiveIntegralNum(requestInfo.getGiveIntegralNum());
                    IntegralGiveConsume.setDeptId(admin.getDeptId());
                    IntegralGiveConsume.setConsumeIntegralMaxNum(requestInfo.getConsumeIntegralMaxNum());
                    IntegralGiveConsume.setStatus(1);
                    if (StringUtils.isNotEmpty(requestInfo.getConsumeMode())) {
                        IntegralGiveConsume.setConsumeMode(Long.valueOf(requestInfo.getConsumeMode()));
                    }
                    long insert = mememberIntegralGiveConsumeMapper.insert(IntegralGiveConsume);
                    if (insert > 0) {
                        restResult.setCode(EXCEPTION.SUCCESS.getName());
                        restResult.setData(IntegralGiveConsume);
                        return restResult;
                    }
                }
                mememberIntegralAction.setCreator(admin.getOperator());
                mememberIntegralAction.setCreatorName(admin.getOperateName());
                mememberIntegralAction.setUpdater(admin.getOperator());
                mememberIntegralAction.setUpdaterName(admin.getOperateName());
                mememberIntegralAction.setDeleteFlag(0);
                long insert = mememberIntegralActionMapper.insert(mememberIntegralAction);
                if (insert > 0) {
                    restResult.setCode(EXCEPTION.SUCCESS.getName());
                    restResult.setData(mememberIntegralAction);
                }
                return restResult;
            }
            //评价
            if (null != requestInfo.getObtainMode() && MODE_CODE.EVALUATE.getCode().equals(requestInfo.getObtainMode())) {
                mememberIntegralAction.setActionTypeName(MODE_CODE.EVALUATE.getMessage());
                if (null != requestInfo.getFlag() && 2 == requestInfo.getFlag()) {
                    mememberIntegralAction.setDeptId(admin.getDeptId());
                    mememberIntegralAction.setRuleId(ruleId);
                    mememberIntegralAction.setStatus(1);
                    mememberIntegralAction.setActionType(Integer.parseInt(requestInfo.getObtainMode()));
                    mememberIntegralAction.setGiveIntegralNum(requestInfo.getGiveGrowthNum());
                    mememberIntegralAction.setGiveIntegralMaxNum(requestInfo.getGiveGrowthMaxNum());
                    mememberIntegralAction.setGiveGrowthNum(requestInfo.getGiveGrowthNum());
                    mememberIntegralAction.setCreator(admin.getOperator());
                    mememberIntegralAction.setCreatorName(admin.getOperateName());
                    mememberIntegralAction.setActionType(Integer.parseInt(MODE_CODE.EVALUATE.getCode()));
                    mememberIntegralAction.setActionTypeName(MODE_CODE.EVALUATE.getMessage());
                    mememberIntegralAction.setUpdater(admin.getOperator());
                    mememberIntegralAction.setUpdaterName(admin.getOperateName());
                    mememberIntegralAction.setFlag(2);
                    mememberIntegralAction.setDeleteFlag(0);
                    //无图
                    mememberIntegralAction.setGiveGrowthMaxNum(requestInfo.getGiveGrowthMaxNum());
                    mememberIntegralAction.setGiveGrowthNum(requestInfo.getGiveGrowthNum());
                    long vaLong = mememberIntegralActionMapper.insert(mememberIntegralAction);
                    if (vaLong > 0) {
                        restResult.setCode(EXCEPTION.SUCCESS.getName());
                        restResult.setData(mememberIntegralAction);
                        return restResult;
                    }
                } else if (null != requestInfo.getFlag() && 1 == requestInfo.getFlag()) {
                    mememberIntegralAction.setDeptId(admin.getDeptId());
                    mememberIntegralAction.setRuleId(ruleId);
                    mememberIntegralAction.setStatus(1);
                    mememberIntegralAction.setActionType(Integer.parseInt(requestInfo.getObtainMode()));
                    mememberIntegralAction.setCreator(admin.getOperator());
                    mememberIntegralAction.setCreatorName(admin.getOperateName());
                    mememberIntegralAction.setUpdater(admin.getOperator());
                    mememberIntegralAction.setUpdaterName(admin.getOperateName());
                    mememberIntegralAction.setFlag(1);
                    mememberIntegralAction.setDeleteFlag(0);
                    //有图
                    mememberIntegralAction.setGiveGrowthMaxNum(requestInfo.getHavePictureGiveGrowthNum());
                    mememberIntegralAction.setGiveGrowthNum(requestInfo.getHavePictureMaxNum());
                    mememberIntegralAction.setGiveIntegralNum(requestInfo.getHavePictureMaxNum());
                    mememberIntegralAction.setGiveIntegralMaxNum(requestInfo.getHavePictureGiveGrowthNum());
                    long vaLong = mememberIntegralActionMapper.insert(mememberIntegralAction);
                    if (vaLong > 0) {
                        restResult.setData(mememberIntegralAction);
                        restResult.setCode(EXCEPTION.SUCCESS.getName());
                        return restResult;
                    }
                } else if (null != requestInfo.getFlag() && 3 == requestInfo.getFlag()) {
                    MememberIntegralAction action = new MememberIntegralAction();
                    action.setDeptId(admin.getDeptId());
                    action.setRuleId(ruleId);
                    action.setStatus(1);
                    action.setActionType(Integer.parseInt(requestInfo.getObtainMode()));
                    action.setCreator(admin.getOperator());
                    action.setCreatorName(admin.getOperateName());
                    action.setUpdater(admin.getOperator());
                    action.setUpdaterName(admin.getOperateName());
                    action.setFlag(3);
                    action.setDeleteFlag(0);
                    //有图
                    action.setGiveGrowthMaxNum(requestInfo.getHavePictureGiveGrowthNum());
                    action.setGiveGrowthNum(requestInfo.getHavePictureMaxNum());
                    action.setGiveIntegralNum(requestInfo.getHavePictureGiveGrowthNum());
                    action.setGiveIntegralMaxNum(requestInfo.getHavePictureMaxNum());

                    final long insert = mememberIntegralActionMapper.insert(action);
                    if (insert > 0) {
                        MememberGrowthAppraisal mememberGrowthAppraisal = new MememberGrowthAppraisal();
                        MememberGrowthAppraisal mememberGrowthAppraisal1 = new MememberGrowthAppraisal();
                        mememberGrowthAppraisal.setGrowthRuleId(action.getRuleId());
                        mememberGrowthAppraisal.setCreator(admin.getOperator());
                        mememberGrowthAppraisal.setDeptId(admin.getDeptId());
                        mememberGrowthAppraisal.setWhetherPic(3);
                        //all
                        BeanUtil.copy(mememberGrowthAppraisal, mememberGrowthAppraisal1);
                        mememberGrowthAppraisal1.setGiveGrowthVal(requestInfo.getGiveGrowthNum());
                        mememberGrowthAppraisal1.setGiveGrowthMaxNum(requestInfo.getGiveGrowthMaxNum());
                        mememberGrowthAppraisal.setGiveGrowthVal(requestInfo.getHavePictureMaxNum());
                        mememberGrowthAppraisal.setGiveGrowthMaxNum(requestInfo.getHavePictureGiveGrowthNum());
                        final long insert1 = mememberGrowthAppraisalService.insert(mememberGrowthAppraisal);
                        final long insert2 = mememberGrowthAppraisalService.insert(mememberGrowthAppraisal1);
                        if (insert1 > 0 && insert2 > 0) {
                            restResult.setData(mememberGrowthAppraisal1);
                            restResult.setData(mememberGrowthAppraisal);
                            restResult.setData(action);
                            restResult.setCode(EXCEPTION.SUCCESS.getName());
                        }

                    } else {
                        restResult.setCode(EXCEPTION.FAIL.getName());
                    }

                }
            }
        }
        return restResult;
    }

    /**
     * 获取登录人信息
     *
     * @param request
     * @param admin
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    MememberIntegralRule getRequestInfo(MememberIntegralRule request, AdminOperate admin) {
        request.setStatus(1);
        request.setDeptId(admin.getDeptId());
        request.setDeleteFlag(0);
        request.setCreatorName(admin.getOperateName());
        request.setCreator(admin.getOperator());
        request.setUpdater(admin.getOperator());
        request.setUpdaterName(admin.getOperateName());
        return request;
    }

    /**
     * 查询积分规则信息
     *
     * @param request
     * @return
     */
    public MememberIntegralRuleResponse queryMemberIntegralInfo(MememberIntegralRuleRequest request) {
        final Long ruleId = request.getRuleId();
        MememberIntegralRuleResponse mememberIntegralRuleResponse = new MememberIntegralRuleResponse();
        //评价
        if (null != request.getObtainMode() && MODE_CODE.EVALUATE.getCode().equals(request.getObtainMode())) {
            mememberIntegralRuleResponse = mememberIntegralActionMapper.queryMememberIntegralActionByRuleId(ruleId);
            MememberGrowthAppraisal mememberGrowthAppraisal = new MememberGrowthAppraisal();
            if (mememberIntegralRuleResponse != null) {
                if (mememberIntegralRuleResponse.getFlag() == 3) {
                    mememberGrowthAppraisal.setGrowthRuleId(mememberIntegralRuleResponse.getId());
                    mememberGrowthAppraisal.setWhetherPic(3);
                    mememberGrowthAppraisal.setDeleteFlag(0);
                    final List<MememberGrowthAppraisal> query = mememberGrowthAppraisalService.query(mememberGrowthAppraisal);
                    if (query.size() > 0) {
                        for (int i = 0; i < query.size(); i++) {
                            mememberIntegralRuleResponse.setGiveGrowthMaxNum(query.get(1).getGiveGrowthMaxNum());
                            mememberIntegralRuleResponse.setGiveGrowthNum(query.get(1).getGiveGrowthVal());
                            mememberIntegralRuleResponse.setHavePictureMaxNum(query.get(0).getGiveGrowthVal());
                            mememberIntegralRuleResponse.setHavePictureGiveGrowthNum(query.get(0).getGiveGrowthMaxNum());
                        }
                        mememberIntegralRuleResponse.setFlag(3);
                    }
                }
            }

        }

        //分享点赞嗮单收藏赠送
        if (MODE_CODE.COLLECTION.getCode().equals(request.getObtainMode()) || MODE_CODE.SUN.getCode().equals(request.getObtainMode())
                || MODE_CODE.PRAISE.getCode().equals(request.getObtainMode()) || MODE_CODE.SHARE.getCode().equals(request.getObtainMode()) || (StringUtils.isNotEmpty(request.getObtainMode()) && Long.valueOf(request.getObtainMode()) > 8
                || (StringUtils.isNotEmpty(request.getConsumeMode()) && Long.valueOf(request.getConsumeMode()) > 4))) {
            mememberIntegralRuleResponse = mememberIntegralActionMapper.queryMememberIntegralActionByRuleId(ruleId);
        }
        /**
         * 下单 充值赠送
         */
        if (MODE_CODE.ORDER.getCode().equals(request.getObtainMode()) || MODE_CODE.RECHARGE.getCode().equals(request.getObtainMode())) {
            mememberIntegralRuleResponse = mememberIntegralOrderRechargeMapper.queryMememberIntegralOrderRechargeByRuleId(ruleId);
            if (mememberIntegralRuleResponse != null) {
                if (mememberIntegralRuleResponse.getConsumeFlag() == 2) {
                    MememberIntegralOrderIncrement mememberIntegralOrderIncrement = new MememberIntegralOrderIncrement();
                    mememberIntegralOrderIncrement.setOrderRechargeId(mememberIntegralRuleResponse.getId());
                    final List<MememberIntegralOrderIncrement> infos = mememberIntegralOrderIncrementMapper.query(mememberIntegralOrderIncrement);
                    List<MememberIntegraIncrement> list = new ArrayList<>();
                    for (int i = 0; i < infos.size(); i++) {
                        MememberIntegraIncrement ememberIntegraIncrement = new MememberIntegraIncrement();
                        ememberIntegraIncrement.setConsumeNum(infos.get(i).getConsumeNum());
                        ememberIntegraIncrement.setGiveIntegralNum(infos.get(i).getGiveIntegralNum());
                        list.add(ememberIntegraIncrement);
                    }
                    mememberIntegralRuleResponse.setMememberIntegraIncrement(list);
                }
            }

        }
        /**
         * 注册赠送/消费抵现
         */
        if (MODE_CODE.GIVE.getCode().equals(request.getObtainMode()) || CONSUME_CODE.ORDER.getCode().equals(request.getConsumeMode())
                || (StringUtils.isNotEmpty(request.getConsumeMode()) && Long.valueOf(request.getConsumeMode()) > 4)
                || (StringUtils.isNotEmpty(request.getObtainMode()) && Long.valueOf(request.getObtainMode()) > 8)) {

            mememberIntegralRuleResponse = mememberIntegralGiveConsumeMapper.querymememberIntegralGiveConsumeByRuleId(ruleId);
            mememberIntegralRuleResponse.setConsumeMode(mememberIntegralRuleResponse.getConsumeMode());
        }
        /**
         * 红包兑换
         */
        if (null != request.getConsumeMode() && CONSUME_CODE.GIVE.getCode().equals(request.getConsumeMode())
                || null != request.getConsumeMode() && CONSUME_CODE.COUPON.getCode().equals(request.getConsumeMode())) {
            mememberIntegralRuleResponse = mememberIntegralExchangeCouponMapper.queryMememberIntegralExchangeCouponByRuleId(ruleId);
        }

        if(mememberIntegralRuleResponse!=null){
            MememberIntegralRule  rule = mememberIntegralRuleMapper.queryByPrimaryKey(mememberIntegralRuleResponse.getRuleId());
            if(rule ==null){
                throw new RuntimeException("查询异常");
            }
            mememberIntegralRuleResponse.setRuleName(rule.getRuleName());
        }

        return mememberIntegralRuleResponse;
    }

    /**
     * 编辑修改积分信息
     *
     * @param request
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public RestResult editMemberIntegraInfo(EditMememberIntegralRuleRequest request) {
        RestResult restResult = new RestResult();
        final MememberIntegralBase mememberIntegralBaseinfo = getMememberIntegralBaseinfo();

        if (null != request.getRuleName() && !"".equals(request.getRuleName())) {
            MememberIntegralRule mememberIntegralRule = new MememberIntegralRule();
            mememberIntegralRule.setRuleId(request.getRuleId());
            mememberIntegralRule.setRuleName(request.getRuleName());
            mememberIntegralRule.setUpdateTime(new Date());
            final long update = mememberIntegralRuleMapper.update(mememberIntegralRule);
        }

        //评价赠送
        if (request.getObtainMode().equals(MODE_CODE.EVALUATE.getCode())) {
            MememberIntegralAction integralAction = new MememberIntegralAction();
            MememberGrowthAppraisal mememberGrowthAppraisal = new MememberGrowthAppraisal();
            MememberGrowthAppraisal mememberGrowthAppraisal1 = new MememberGrowthAppraisal();
            final Long ruleId = request.getRuleId();

            integralAction.setRuleId(ruleId);
            final List<MememberIntegralAction> query = mememberIntegralActionMapper.query(integralAction);
            if (null != query) {
                for (MememberIntegralAction mememberIntegralAction : query) {
                    Long id = mememberIntegralAction.getId();
                    if (null != request.getFlag() && request.getFlag() == 3) {
                        mememberGrowthAppraisal.setGrowthRuleId(id);
                        mememberGrowthAppraisal.setWhetherPic(3);
                        final List<MememberGrowthAppraisal> queryinfo = mememberGrowthAppraisalService.query(mememberGrowthAppraisal);
                        if (null != queryinfo && queryinfo.size() > 0) {
                            for (MememberGrowthAppraisal growthAppraisal : queryinfo) {
                                final long delete = mememberGrowthAppraisalService.deleteByRuleId(growthAppraisal.getGrowthRuleId());
                                if (delete > 0) {
                                    mememberGrowthAppraisal.setGrowthRuleId(id);
                                    mememberGrowthAppraisal.setCreator(request.getAdminOperate().getOperator());
                                    mememberGrowthAppraisal.setDeptId(request.getAdminOperate().getDeptId());
                                    mememberGrowthAppraisal.setWhetherPic(3);
                                    BeanUtil.copy(mememberGrowthAppraisal, mememberGrowthAppraisal1);
                                    mememberGrowthAppraisal1.setGiveGrowthVal(request.getGiveGrowthNum());
                                    mememberGrowthAppraisal1.setGiveGrowthMaxNum(request.getGiveGrowthMaxNum());
                                    mememberGrowthAppraisal.setGiveGrowthVal(request.getHavePictureMaxNum());
                                    mememberGrowthAppraisal.setGiveGrowthMaxNum(request.getHavePictureGiveGrowthNum());
                                    final long insert = mememberGrowthAppraisalService.insert(mememberGrowthAppraisal);
                                    final long insert1 = mememberGrowthAppraisalService.insert(mememberGrowthAppraisal1);
                                    if (insert > 0 && insert1 > 0) {
                                        restResult.setCode(EXCEPTION.SUCCESS.getName());
                                        restResult.setMsg(EXCEPTION.SUCCESS.getDesc());
                                        return restResult;
                                    }
                                }
                            }
                        } else if (null != queryinfo && queryinfo.size() == 0) {
                            mememberGrowthAppraisal.setGrowthRuleId(id);
                            mememberGrowthAppraisal.setCreator(request.getAdminOperate().getOperator());
                            mememberGrowthAppraisal.setDeptId(request.getAdminOperate().getDeptId());
                            mememberGrowthAppraisal.setWhetherPic(3);
                            BeanUtil.copy(mememberGrowthAppraisal, mememberGrowthAppraisal1);
                            mememberGrowthAppraisal1.setGiveGrowthVal(request.getGiveGrowthNum());
                            mememberGrowthAppraisal1.setGiveGrowthMaxNum(request.getGiveGrowthMaxNum());
                            mememberGrowthAppraisal.setGiveGrowthVal(request.getHavePictureMaxNum());
                            mememberGrowthAppraisal.setGiveGrowthMaxNum(request.getHavePictureGiveGrowthNum());
                            final long insert = mememberGrowthAppraisalService.insert(mememberGrowthAppraisal);
                            final long insert1 = mememberGrowthAppraisalService.insert(mememberGrowthAppraisal1);
                            if (insert > 0 && insert1 > 0) {
                                restResult.setCode(EXCEPTION.SUCCESS.getName());
                                restResult.setMsg(EXCEPTION.SUCCESS.getDesc());
                                return restResult;
                            }
                        }
                    } else if (null != request.getFlag() || request.getFlag() == 1 || request.getFlag() == 2) {
                        MememberIntegralAction memberRequest = BeanUtil.copy(request, MememberIntegralAction.class);
                        memberRequest.setUpdaterName(request.getAdminOperate().getOperateName());
                        memberRequest.setDeptId(request.getDeptId());
                        memberRequest.setId(id);
                        if (request.getFlag() == 1) {
                            memberRequest.setGiveIntegralNum(request.getHavePictureMaxNum());
                            memberRequest.setGiveIntegralMaxNum(request.getHavePictureGiveGrowthNum());
                            memberRequest.setGiveGrowthNum(request.getHavePictureMaxNum());
                            memberRequest.setGiveGrowthMaxNum(request.getHavePictureGiveGrowthNum());
                        }
                        if (request.getFlag() == 2) {
                            memberRequest.setGiveIntegralNum(request.getGiveGrowthNum());
                            memberRequest.setGiveIntegralMaxNum(request.getGiveIntegralMaxNum());
                            memberRequest.setGiveGrowthNum(request.getGiveGrowthNum());
                            memberRequest.setGiveGrowthMaxNum(request.getGiveGrowthMaxNum());
                            memberRequest.setHavePictureGiveGrowthNum(request.getHavePictureGiveGrowthNum());
                        }

                        final Long update = mememberIntegralActionMapper.update(memberRequest);
                        if (update > 0) {
                            restResult.setData(memberRequest);
                            restResult.setCode(EXCEPTION.SUCCESS.getName());
                            restResult.setMsg(EXCEPTION.SUCCESS.getDesc());
                            return restResult;
                        }
                    }
                }
            }
            return restResult;
        }
        /**
         * 点赞赠送 收藏赠送 嗮单赠送
         */
        if (request.getObtainMode().equals(MODE_CODE.PRAISE.getCode()) || request.getObtainMode().equals(MODE_CODE.SUN.getCode())
                || request.getObtainMode().equals(MODE_CODE.COLLECTION.getCode()) || request.getObtainMode().equals(MODE_CODE.SHARE.getCode())
                || (StringUtils.isNotEmpty(request.getObtainMode()) && Long.valueOf(request.getObtainMode()) > 8)) {
            MememberIntegralAction integralAction = new MememberIntegralAction();
            final Long ruleId = request.getRuleId();
            integralAction.setRuleId(ruleId);
            final List<MememberIntegralAction> query = mememberIntegralActionMapper.query(integralAction);
            if (query != null) {
                MememberIntegralAction memberRequest = BeanUtil.copy(request, MememberIntegralAction.class);
                memberRequest.setUpdaterName(request.getAdminOperate().getOperateName());
                memberRequest.setDeptId(request.getDeptId());
                query.forEach(items -> {
                    memberRequest.setId(items.getId());
                    memberRequest.setGiveIntegralNum(request.getGiveIntegralNum());
                    memberRequest.setGiveIntegralMaxNum(request.getGiveIntegralMaxNum());
                    final Long update = mememberIntegralActionMapper.update(memberRequest);
                    if (update != null) {
                        restResult.setData(memberRequest);
                        restResult.setCode(EXCEPTION.SUCCESS.getName());
                        restResult.setMsg(EXCEPTION.SUCCESS.getDesc());
                    }
                });

                return restResult;
            }
        }
        /**
         * 注册赠送 消费抵现
         */
        if (MODE_CODE.GIVE.getCode().equals(request.getObtainMode()) || CONSUME_CODE.ORDER.getCode().equals(request.getConsumeMode())
                || (StringUtils.isNotEmpty(request.getConsumeMode()) && Long.valueOf(request.getConsumeMode()) > 4)) {
            MememberIntegralGiveConsume mememberIntegralGiveConsume = new MememberIntegralGiveConsume();
            final Long ruleId = request.getRuleId();
            mememberIntegralGiveConsume.setRuleId(ruleId);
            final List<MememberIntegralGiveConsume> query = mememberIntegralGiveConsumeMapper.query(mememberIntegralGiveConsume);
            if (query != null && query.size() > 0) {
                MememberIntegralGiveConsume giveConsumeRequest = BeanUtil.copy(request, MememberIntegralGiveConsume.class);
                giveConsumeRequest.setUpdaterName(request.getAdminOperate().getOperateName());
                giveConsumeRequest.setUpdater(request.getAdminOperate().getBusinessId());
                query.forEach(items -> {
                    giveConsumeRequest.setId(items.getId());
                    final Long update = mememberIntegralGiveConsumeMapper.update(giveConsumeRequest);
                    if (update != null) {
                        restResult.setData(giveConsumeRequest);
                        restResult.setCode(EXCEPTION.SUCCESS.getName());
                        restResult.setMsg(EXCEPTION.SUCCESS.getDesc());
                    }
                });
                return restResult;
            }
        }
        /**
         * 兑换优惠券或红包
         */
        if (null != request.getConsumeMode() && request.getConsumeMode().equals(CONSUME_CODE.GIVE.getCode())
                || null != request.getConsumeMode() && request.getConsumeMode().equals(CONSUME_CODE.COUPON.getCode())) {
            final Long ruleId = request.getRuleId();
            MememberIntegralExchangeCoupon info = mememberIntegralExchangeCouponMapper.queryByRuleId(ruleId);
            final int exchangeType = info.getExchangeType();
            if (null != request.getExchangeType() && request.getExchangeType() == exchangeType) {
                MememberIntegralExchangeCoupon gorUpon = BeanUtil.copy(request, MememberIntegralExchangeCoupon.class);
                gorUpon.setId(info.getId());
                gorUpon.setRuleId(ruleId);
                gorUpon.setStatus(1);
                gorUpon.setDeleteFlag(0);
                final long insert = mememberIntegralExchangeCouponMapper.update(gorUpon);
                if (insert > 0) {
                    restResult.setCode(EXCEPTION.SUCCESS.getName());
                    restResult.setMsg(EXCEPTION.SUCCESS.getDesc());
                }
            }
            if (null != request.getExchangeType() && exchangeType != request.getExchangeType()) {
                MememberIntegralExchangeCoupon gorUpons = BeanUtil.copy(request, MememberIntegralExchangeCoupon.class);
                gorUpons.setId(info.getId());
                gorUpons.setDeptId(request.getAdminOperate().getDeptId());
                gorUpons.setRuleId(ruleId);
                gorUpons.setExchangeType(request.getExchangeType());
                gorUpons.setStatus(1);
                gorUpons.setDeleteFlag(0);
                gorUpons.setDeptId(request.getAdminOperate().getDeptId());
                gorUpons.setCreatorName(request.getAdminOperate().getOperateName());
                gorUpons.setUpdaterName(request.getAdminOperate().getOperateName());
                final long insert = mememberIntegralExchangeCouponMapper.update(gorUpons);
                if (insert > 0) {
                    restResult.setCode(EXCEPTION.SUCCESS.getName());
                    restResult.setMsg(EXCEPTION.SUCCESS.getDesc());
                }
                return restResult;
            }
            return restResult;
        }

        /**
         * 下单 充值
         */
        if (MODE_CODE.ORDER.getCode().equals(request.getObtainMode()) || MODE_CODE.RECHARGE.getCode().equals(request.getObtainMode())) {
            final Long ruleId = request.getRuleId();
            final List<MememberIntegralOrderRecharge> mememberIntegralOrderRecharges = mememberIntegralOrderRechargeMapper.queryByRuleId(ruleId);
            MememberIntegralOrderIncrement orderInt = new MememberIntegralOrderIncrement();
            MememberIntegralOrderRecharge orderRecharge = new MememberIntegralOrderRecharge();
            if (null != request.getOrderFlag() && request.getOrderFlag() == 2 || null != request.getConsumeFlag() && request.getConsumeFlag() == 2) {
                if (mememberIntegralOrderRecharges != null) {
                    for (MememberIntegralOrderRecharge mememberIntegralOrderRecharge : mememberIntegralOrderRecharges) {
                        Long orderRechargeId = mememberIntegralOrderRecharge.getId();
                        final long delete = mememberIntegralOrderRechargeMapper.delete(orderRechargeId);
                        orderRecharge.setRuleId(ruleId);
                        orderRecharge.setGiveType(2);
                        orderRecharge.setDeptId(request.getAdminOperate().getDeptId());
                        orderRecharge.setGiveIntegralMaxNum(request.getGiveIntegralMaxNum());
                        orderRecharge.setConsumeFlag(2);
                        orderRecharge.setStatus(1);
                        if (null != request.getOrderFlag() && request.getOrderFlag() == 2) {
                            orderRecharge.setGiveTypeName(MODE_CODE.ORDER.getMessage());
                        } else {
                            orderRecharge.setGiveTypeName(MODE_CODE.RECHARGE.getMessage());
                        }
                        orderRecharge.setDeleteFlag(0);
                        orderRecharge.setCreatorName(request.getAdminOperate().getOperateName());
                        orderRecharge.setCreator(request.getAdminOperate().getBusinessId());
                        final long insert = mememberIntegralOrderRechargeMapper.insert(orderRecharge);
                        List<MememberIntegraIncrement> mememberIntegraIncrement = request.getMememberIntegraIncrement();

                        List<MememberIntegraIncrement> list = new ArrayList<>();
                        for (int i = 0; i < mememberIntegraIncrement.size(); i++) {
                            if (null != mememberIntegraIncrement.get(i).getGiveIntegralNum() && null != mememberIntegraIncrement.get(i).getConsumeNum()) {
                                list.add(mememberIntegraIncrement.get(i));
                            }
                        }
                        orderInt.setDeptId(request.getAdminOperate().getDeptId());
                        orderInt.setOrderRechargeId(orderRecharge.getOrderRechargeId());
                        orderInt.setCreatorName(request.getAdminOperate().getOperateName());
                        orderInt.setUpdaterName(request.getAdminOperate().getOperateName());
                        orderInt.setGiveIntegralMaxNum(request.getGiveIntegralMaxNum());
                        orderInt.setStatus(1);
                        orderInt.setGiveIntegralMaxNum(request.getGiveIntegralMaxNum());
                        orderInt.setRuleId(ruleId);
                        orderInt.setDeleteFlag(0);

                        list.forEach(items -> {
                            orderInt.setConsumeNum(items.getConsumeNum());
                            orderInt.setGiveIntegralNum(items.getGiveIntegralNum());
                            mememberIntegralOrderIncrementMapper.insert(orderInt);
                        });
                    }
                    restResult.setData(orderInt);
                    restResult.setCode(EXCEPTION.SUCCESS.getName());
                    restResult.setMsg(EXCEPTION.SUCCESS.getDesc());
                }
                restResult.setCode(EXCEPTION.SUCCESS.getName());
            } else if (null != request.getOrderFlag() && request.getOrderFlag() == 1 || null != request.getConsumeFlag() && request.getConsumeFlag() == 1) {
                MememberIntegralOrderRecharge orderIntInfo = new MememberIntegralOrderRecharge();
                for (MememberIntegralOrderRecharge integralOrderRecharge : mememberIntegralOrderRecharges) {
                    orderIntInfo.setRuleId(integralOrderRecharge.getRuleId());
                    final List<MememberIntegralOrderRecharge> query = mememberIntegralOrderRechargeMapper.query(orderIntInfo);
                    orderInt.setId(integralOrderRecharge.getOrderRechargeId());
                    for (MememberIntegralOrderRecharge orderOrderRecha : query) {
                        final Long id = orderOrderRecha.getId();
                        mememberIntegralOrderRechargeMapper.delete(id);
                    }

                    orderIntInfo.setDeptId(request.getAdminOperate().getDeptId());
                    orderIntInfo.setRuleId(ruleId);
                    orderIntInfo.setOrderRechargeId(ruleId);
                    orderIntInfo.setConsumeFlag(1);
                    orderIntInfo.setConsumeNum(request.getConsumeNum());
                    orderIntInfo.setGiveIntegralNum(request.getGiveIntegralNum());
                    orderIntInfo.setGiveGrowthMaxNum(request.getGiveGrowthMaxNum());
                    orderIntInfo.setGiveIntegralMaxNum(request.getGiveIntegralMaxNum());
                    orderIntInfo.setCreator(request.getAdminOperate().getOperator());
                    orderIntInfo.setGiveType(1);
                    orderIntInfo.setConsumeFlag(1);
                    if (null != request.getOrderFlag() && 1 == request.getOrderFlag()) {
                        orderIntInfo.setGiveTypeName(MODE_CODE.ORDER.getMessage());
                    } else {
                        orderIntInfo.setGiveTypeName(MODE_CODE.RECHARGE.getMessage());
                    }
                    orderIntInfo.setCreatorName(request.getAdminOperate().getOperateName());
                    orderIntInfo.setUpdater(request.getAdminOperate().getOperator());
                    orderIntInfo.setUpdaterName(request.getAdminOperate().getOperateName());
                    orderIntInfo.setStatus(1);
                    orderIntInfo.setDeleteFlag(0);
                    final long value = mememberIntegralOrderRechargeMapper.insert(orderIntInfo);
                    restResult.setData(orderIntInfo);
                }
                restResult.setCode(EXCEPTION.SUCCESS.getName());
                restResult.setMsg(EXCEPTION.SUCCESS.getDesc());
            }

        }
        return restResult;
    }

    /**
     * 删除积分信息
     *
     * @param form
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public RestResult deletMemberIntegralInfo(MememberIntegralRuleUpdateRequest form) {
        RestResult restResult = new RestResult();;
        MememberIntegralRule memberRequest = BeanUtil.copy(form, MememberIntegralRule.class);
        long update = mememberIntegralRuleMapper.updateDeleteFlag(memberRequest);
        if (update > 0) {
            restResult.setCode(EXCEPTION.SUCCESS.getName());
            restResult.setMsg(EXCEPTION.SUCCESS.getDesc());
        }
        return restResult;
    }


    public RestResult stopStatus(MememberIntegralRuleUpdateRequest form) {
        RestResult restResult = new RestResult();
        final String obtainMode = form.getObtainMode();
        if (null != obtainMode || null != form.getConsumeMode() && !"".equals(form.getConsumeMode()) && "1".equals(form.getConsumeMode())) {
            MememberIntegralTypeRequest memberRequest = BeanUtil.copy(form, MememberIntegralTypeRequest.class);
            memberRequest.setDeptId(form.getAdminOperate().getDeptId());
            memberRequest.setConsumeMode(form.getConsumeMode());
            final Long aLong = mememberIntegralRuleMapper.queryIntegraRuleInfo(memberRequest);
            if (aLong > 0) {
                restResult.setMsg("该类型存在正在启用的规则，请先停用后再进行操作");
                restResult.setCode(EXCEPTION.FAIL.getName());
                return restResult;
            }
        }
        MememberIntegralRule memberRequest = BeanUtil.copy(form, MememberIntegralRule.class);
        final Long aLong = mememberIntegralRuleMapper.stopStatus(memberRequest);
        if (aLong > 0) {
            restResult.setCode(EXCEPTION.SUCCESS.getName());
        }

        return restResult;

    }

    /**
     * 获取会员积分基础信息
     *
     * @return
     */
    private MememberIntegralBase getMememberIntegralBaseinfo() {
        MememberIntegralBase mememberIntegralBase = new MememberIntegralBase();
        mememberIntegralBase.setDeleteFlag(0);
        mememberIntegralBase.setStatus(1);
        mememberIntegralBase.setMaxGrowthValue(1);
        final MememberIntegralBase query = mememberIntegralBaseMapper.queryInfo(mememberIntegralBase);
        if (null != query && query.getId() != null) {
            return query;
        }
        return null;
    }

}